<template>
	<view class="login-container">
		<view class="login-card">
			<text class="card-title">登录</text>
			<view class="form-item">
				<text class="label">姓名：</text>
				<input type="text" v-model="name" placeholder="请输入姓名" class="input" />
			</view>
			<view class="form-item">
				<text class="label">密码：</text>
				<input type="password" v-model="password" placeholder="请输入密码" class="input" />
			</view>
			<button class="login-btn" @click="login" :disabled="isLoading">
				{{ isLoading ? '登录中...' : '登录' }}
			</button>
			<navigator url="/pages/register/register" class="register-link">还没有账号？去注册</navigator>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name: '', 
				password: '',
				isLoading: false 
			};
		},
		methods: {
			async login() {
				if (!this.name.trim() || !this.password.trim()) {
					uni.showToast({
						title: '姓名或密码不能为空',
						icon: 'none'
					});
					return;
				}

				try {
					this.isLoading = true;

					const res = await uniCloud.callFunction({
						name: 'checkUser',
						data: {
							name: this.name.trim(),
							password: this.password.trim()
						}
					});

					const {
						code,
						data,
						message
					} = res.result;
					switch (code) {
						case 200:
							uni.setStorageSync('userId', data);
							uni.showToast({
								title: '登录成功',
								icon: 'success'
							});
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/tabBar/tabcomPage/tabcomPage'
								});
							}, 1000);
							break;
						case 201:
							uni.showToast({
								title: data || '密码错误',
								icon: 'none'
							});
							break;
						case 300:
							uni.showToast({
								title: data || '用户不存在',
								icon: 'none'
							});
							break;
						case 400:
							uni.showToast({
								title: message || '姓名或密码格式错误',
								icon: 'none'
							});
							break;
						case 500:
							uni.showToast({
								title: message || '服务器出错，请稍后重试',
								icon: 'none'
							});
							break;
						case 600:
							uni.showToast({
								title: data || '数据异常，请联系管理员',
								icon: 'none'
							});
							break;
						default:
							uni.showToast({
								title: '登录失败，请重试',
								icon: 'none'
							});
					}
				} catch (error) {
					console.error('登录请求失败：', error);
					uni.showToast({
						title: '网络异常，请检查网络',
						icon: 'none'
					});
				} finally {
					this.isLoading = false;
				}
			}
		}
	};
</script>

<style scoped>
	.login-container {
		display: flex;
		justify-content: center;
		align-items: center;
		min-height: 100vh;
		background-color: #f5f7fa;
	}

	.login-card {
		width: 80%;
		max-width: 400px;
		background-color: #fff;
		padding: 40rpx;
		border-radius: 12rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
	}

	.card-title {
		display: block;
		font-size: 36rpx;
		font-weight: bold;
		text-align: center;
		margin-bottom: 40rpx;
		color: #333;
	}

	.form-item {
		display: flex;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.label {
		width: 120rpx;
		font-size: 28rpx;
		color: #666;
	}

	.input {
		flex: 1;
		border: 1px solid #ddd;
		border-radius: 6rpx;
		padding: 15rpx;
		font-size: 28rpx;
		transition: border-color 0.3s;
	}

	.input:focus {
		border-color: #007AFF;
	}

	.login-btn {
		width: 100%;
		background-color: #007AFF;
		color: #fff;
		border-radius: 6rpx;
		font-size: 28rpx;
		padding: 20rpx 0;
		margin-top: 20rpx;
	}

	.login-btn:active {
		opacity: 0.8;
	}

	.login-btn[disabled] {
		background-color: #999;
		opacity: 1;
	}

	.register-link {
		display: block;
		text-align: center;
		font-size: 26rpx;
		color: #007AFF;
		margin-top: 20rpx;
	}
</style>